﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：易焊
    /// </summary>
    public class R004: BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;
            int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //1.Customer:
                    if(sheet.GetRow(4).GetCell(headColumnsIndex + 1) != null)
                    sheet.GetRow(4).GetCell(headColumnsIndex+1).SetCellValue(head.CustomerCode);

                    sheet.GetRow(5).GetCell(headColumnsIndex+1).SetCellValue(head.LotNo);
                    sheet.GetRow(5).GetCell(headColumnsIndex + 6).SetCellValue(
                        string.Format("{0} {1}uF/{2}V"
                        ,head.Series
                        ,head.Cap.ToString()
                        ,head.Voltage.ToString()
                        )
                        );
                    sheet.GetRow(5).GetCell(headColumnsIndex + 9).SetCellValue(head.Size);

                    //'Condition : Temperature   :  '260±5℃
                    if(head.TestTemp>0)
                        sheet.GetRow(7).GetCell(headColumnsIndex + 0).SetCellValue(
                            string.Format("Condition : Temperature   :  {0}±5℃", head.TestTemp.ToString())
                            );

                    // less than  176 μA 
                    sheet.GetRow(7).GetCell(headColumnsIndex + 6).SetCellValue(
                            string.Format("less than  {0} μA", head.LcMax.ToString())
                            );

                    sheet.GetRow(8).GetCell(headColumnsIndex + 6).SetCellValue(
                            head.Standard.CapMin.ToString() 
                            + "-" + head.Standard.CapMax.ToString()
                            );
                    //'Initial value± 10 %
                    sheet.GetRow(8).GetCell(headColumnsIndex + 8).SetCellValue(
                            string.Format("Initial value ±{0}%", head.CapRate.ToString())
                            );

                    //'Initial value± 10 %
                    sheet.GetRow(9).GetCell(headColumnsIndex + 7).SetCellValue(head.DfMax / 100d);
                    if(head.DfMaxAfter >0)
                    {
                        sheet.GetRow(9).GetCell(headColumnsIndex + 9).SetCellFormula(string.Empty);
                        sheet.GetRow(9).GetCell(headColumnsIndex + 9).SetCellValue(head.DfMaxAfter / 100d);
                    }

                    if(head.Dtls.Count>0)
                    {
                        sheet.GetRow(15).GetCell(headColumnsIndex + 1).SetCellValue(head.Dtls[0].TestDate.ToString("yyyy/M/d"));
                    }
                    else
                    {
                        sheet.GetRow(15).GetCell(headColumnsIndex + 1).SetCellValue(head.TestDate.ToString("yyyy/M/d"));
                    }

                    //Tested by:王燕
                    sheet.GetRow(30).GetCell(headColumnsIndex + 8).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }



                headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 19;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 16;
            int dtlColumns = 4;
            int dtlRowsCount = 9;

            string RT = "RT:   ";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 1) break;

                try
                {

                    if (i == 0)
                    {
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                            RT + head.Dtls[i].GetTemp().Replace("℃", string.Empty)
                            );

                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 2).SetCellValue(
                            head.Dtls[i].GetHumidity().Replace("%", string.Empty)
                            );
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if(carpss[k].Value!= 0) 
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                if (i > 0) cIndex += 2;
                else cIndex++;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                    }
                }

                cIndex++;
                var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }


                cIndex++;
                var l1 = head.Dtls[i].Datas.Where(d => d.ItemCode == "L1");
                if (l1.Any())
                {
                    var l1s = l1.ToList();
                    for (int k = 0; k < l1s.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (!string.IsNullOrWhiteSpace(l1s[k].Valid) && OrtReportFactory.ValidStringList.Contains(l1s[k].Valid))
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(l1s[k].Valid);
                    }
                }

                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;
            }

            #endregion

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
